/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.report;

import com.thienloc.omap.controller.BaseBean;
import com.thienloc.omap.entity.report.ShortCodeProduction;
import com.thienloc.omap.session.RequestQueueFacade;
import com.thienloc.omap.util.JsfUtil;
import java.io.Serializable;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class ProductionByShortCodeBean extends BaseBean implements Serializable {

    @EJB
    private RequestQueueFacade requestQueueFacade;
    private String year;
    private String month;
    private Date date;
    private Boolean byMonth;
    private List<ShortCodeProduction> productionMonths = new ArrayList<ShortCodeProduction>();
    private List<ShortCodeProduction> productionDays = new ArrayList<ShortCodeProduction>();
    private final String SQL_DATE_PATTERN_MONTH = "yyyy-MM";
    private final String SQL_DATE_PATTERN_DAY = "yyyy-MM-dd";
    private final Logger logger = LoggerFactory.getLogger(ProductionByShortCodeBean.class);

    @PostConstruct
    public void init() {
        try {
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", "Lỗi");
        }
    }

    public void reportByThisMonth() {
        Calendar cal = Calendar.getInstance();
        year = String.valueOf(cal.get(Calendar.YEAR));
        month = String.valueOf(cal.get(Calendar.MONTH) + 1);
        reportByMonth();
    }

    public void reportByMonth() {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, Integer.parseInt(year));
            calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);

            productionMonths = requestQueueFacade.countByShortCode(calendar.getTime(), SQL_DATE_PATTERN_MONTH);

            ShortCodeProduction total = new ShortCodeProduction();
            total.setOpertator("Tổng");
            for (ShortCodeProduction shortCodeProduction : productionMonths) {
                total.setP6071(shortCodeProduction.getP6071() + total.getP6071());
                total.setP6171(shortCodeProduction.getP6171() + total.getP6171());
                total.setP6271(shortCodeProduction.getP6271() + total.getP6271());
                total.setP6371(shortCodeProduction.getP6371() + total.getP6371());
                total.setP6471(shortCodeProduction.getP6471() + total.getP6471());
                total.setP6571(shortCodeProduction.getP6571() + total.getP6571());
                total.setP6671(shortCodeProduction.getP6671() + total.getP6671());
                total.setP6771(shortCodeProduction.getP6771() + total.getP6771());
            }
            productionMonths.add(total);
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", ex.getMessage());
        }
    }

    public void reportByToday() {
        date = new Date();
        reportByDay();
    }

    public void reportByDay() {
        try {
            productionDays = requestQueueFacade.countByShortCode(date, SQL_DATE_PATTERN_DAY);
            ShortCodeProduction total = new ShortCodeProduction();
            total.setOpertator("Tổng");
            for (ShortCodeProduction shortCodeProduction : productionDays) {
                total.setP6071(shortCodeProduction.getP6071() + total.getP6071());
                total.setP6171(shortCodeProduction.getP6171() + total.getP6171());
                total.setP6271(shortCodeProduction.getP6271() + total.getP6271());
                total.setP6371(shortCodeProduction.getP6371() + total.getP6371());
                total.setP6471(shortCodeProduction.getP6471() + total.getP6471());
                total.setP6571(shortCodeProduction.getP6571() + total.getP6571());
                total.setP6671(shortCodeProduction.getP6671() + total.getP6671());
                total.setP6771(shortCodeProduction.getP6771() + total.getP6771());
            }
            productionDays.add(total);
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", ex.getMessage());
        }
    }

    public void reportByService() {
    }

    public void reportByOperator() {
    }

    public Boolean getByMonth() {
        return byMonth;
    }

    public void setByMonth(Boolean byMonth) {
        this.byMonth = byMonth;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public List<ShortCodeProduction> getProductionDays() {
        return productionDays;
    }

    public void setProductionDays(List<ShortCodeProduction> productionDays) {
        this.productionDays = productionDays;
    }

    public List<ShortCodeProduction> getProductionMonths() {
        return productionMonths;
    }

    public void setProductionMonths(List<ShortCodeProduction> productionMonths) {
        this.productionMonths = productionMonths;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }
}
